package com.yf.controller.ai.manage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.log.annotation.OperationLog;
import com.yf.model.ai.form.AiToolForm;
import com.yf.model.ai.query.AiToolPageQuery;
import com.yf.model.ai.vo.AiToolPageVO;
import com.yf.model.monitor.enums.BusinessTypeEnum;
import com.yf.rate_limiting.annotation.PreventDuplicateSubmit;
import com.yf.result.PageResult;
import com.yf.result.Result;
import com.yf.service.manage.IAiToolService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * AI 工具-AiToolController
 * TODO 前端新增 / 修改 不美观
 *
 * @author: YiFei
 * @since : 2025年7月6日 22:07:48
 */
@Tag(name = "AI 工具")
@RestController
@RequestMapping("ai_tool")
@RequiredArgsConstructor
public class AiToolController {

    private final IAiToolService aiToolService;

    @Operation(summary = "查询 AI 工具")
    @OperationLog(title = "查询 AI 工具", businessType = BusinessTypeEnum.SEARCH)
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:tool:list')")
    @GetMapping("/page")
    public PageResult<AiToolPageVO> getAiToolPage(@ParameterObject @Validated AiToolPageQuery queryParams) {
        IPage<AiToolPageVO> result = aiToolService.getAiToolPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "AI 工具表单数据")
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:tool:list')")
    @GetMapping("/{id}/form")
    public Result<AiToolForm> getAiToolForm(@PathVariable Integer id) {
        AiToolForm aiToolForm = aiToolService.getAiToolForm(id);
        return Result.judge(aiToolForm);
    }

    @Operation(summary = "新增 AI 工具")
    @OperationLog(title = "新增 AI 工具", businessType = BusinessTypeEnum.INSERT)
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:tool:save')")
    @PostMapping
    public Result<Integer> saveAiTool(@RequestBody @Validated AiToolForm aiToolForm) {
        Integer result = aiToolService.saveAiTool(aiToolForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改 AI 工具")
    @OperationLog(title = "修改 AI 工具", businessType = BusinessTypeEnum.UPDATE)
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:tool:update')")
    @PutMapping("/{id}")
    public Result<Void> updateAiTool(@Parameter(description = "AI 工具主键") @PathVariable Integer id, @RequestBody @Validated AiToolForm aiToolForm) {
        boolean deleted = aiToolService.updateAiTool(id, aiToolForm);
        return Result.judge(deleted);
    }

    @Operation(summary = "删除 AI 工具")
    @OperationLog(title = "删除 AI 工具", businessType = BusinessTypeEnum.DELETE)
    @PreventDuplicateSubmit
    @PreAuthorize("@permission.checker('ai:tool:delete')")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteAiTool(@Parameter(description = "{ids 以 , 分隔") @PathVariable String ids) {
        boolean deleted = aiToolService.deleteAiTool(Arrays.stream(ids.split(","))
                .map(Integer::parseInt).collect(Collectors.toList()));
        return Result.judge(deleted);
    }

}
